package com.specialtreasure.freightdepot.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AlertDialog;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.app.core.util.XmlParser;
import com.specialtreasure.freightdepot.R;
import com.specialtreasure.freightdepot.adapter.SelectUnitAdapter;

import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;


/**
 * Created by KUMA on 2017/7/25.
 * 单选任何xml
 */

public class SelectUnitPW extends PopupWindow {
    private Context mContext;
    private String title;
    private GridView gv_gridview;
    private SelectUnitAdapter adapter;
    private TextView tv_cancel;
    private TextView tv_title;
    private OnResultListener mOnResultListener;
    private List<String> data;

    public SelectUnitPW(Context context) {
        super(context);
        this.mContext = context;
        init("danwei.xml");
    }

    public SelectUnitPW(Context context, String xmlName) {
        super(context);
        this.mContext = context;
        init(xmlName);
    }

    public SelectUnitPW(Context context, String xmlName,String title) {
        super(context);
        this.mContext = context;
        this.title = title;
        init(xmlName);
    }



    private void init(String xmlName) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.pw_unit, null);
        gv_gridview = (GridView) contentView.findViewById(R.id.gv_gridview);
        tv_cancel = (TextView) contentView.findViewById(R.id.tv_cancel);
        tv_title = (TextView) contentView.findViewById(R.id.tv_title);


        tv_title.setText(title);
        data = new ArrayList<>();
        try {
            List<String> temp = XmlParser.parseXml(mContext, xmlName);
            data.addAll(temp);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        adapter = new SelectUnitAdapter(mContext, data);
        gv_gridview.setAdapter(adapter);
        gv_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mOnResultListener != null) {
                    String str = data.get(position);
                    if (str.equals("自定义")) {
                        final EditText et = new EditText(mContext);
                        et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});
                        new AlertDialog.Builder(mContext)
                                .setTitle("请输入内容（最多五个字）")
                                .setView(et)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        mOnResultListener.onResult(et.getText().toString());
                                        SelectUnitPW.this.dismiss();
                                    }
                                })
                                .setNegativeButton("取消", null)
                                .create()
                                .show();
                    } else {
                        mOnResultListener.onResult(data.get(position));
                        SelectUnitPW.this.dismiss();
                    }
                }
            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectUnitPW.this.dismiss();
            }
        });
        // 设置SelectPicPopupWindow的View
        this.setContentView(contentView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
//        // 刷新状态
//        this.update();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
    }

    public void showAtBottom(View v) {
        this.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        backgroundAlpha(0.5f);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        Activity activity = (Activity) mContext;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);
    }

    public interface OnResultListener {
        public void onResult(String str);
    }

    public void setOnResultListener(OnResultListener OnResultListener) {
        this.mOnResultListener = OnResultListener;
    }
}
